package com.service.impl;

import com.bean.Project;
import com.bean.Table;
import com.common.ConfigParam;
import com.common.FrameUtil;
import com.dao.ProjectDao;
import com.dao.TableDao;
import com.service.IProjectService;
import com.service.ITableService;

import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;
import java.util.List;

public class ProjectServiceImpl implements IProjectService {

	private boolean oprate = ConfigParam.handleData();

	/**
	 *
	 * 根据条件查询数据，重新加载 model
	 *
	 * @param model
	 */
	public List<Project> loadModelData(DefaultTableModel model) {

		List<Project> datas = new ProjectDao().query(null);
		List<Object[]> dataArrList = new ArrayList<Object[]>();
		for(Project obj : datas){
			dataArrList.add(obj.getArr());
		}
		FrameUtil.refreshTableModelData(model, dataArrList);
		return datas;
	}

	/**
	 * 新增表单记录
	 * @param obj
	 * @return
	 */
	public boolean add(Project obj){
		if(oprate)
			return new ProjectDao().add(obj);
		return oprate;
	}

	/**
	 * 获取实体bean
	 * @param id
	 * @return
	 */
	public Project get(Long id){
		return new ProjectDao().get(id);
	}

	/**
	 * 获取实体bean
	 * @param name
	 * @return
	 */
	public Project getByName(String name){
		if(null != name && !"".equals(name)){
			return new ProjectDao().getByName(name);
		}
		return null;
	}

	/**
	 * 修改实体bean
	 * @param obj
	 * @return
	 */
	public boolean update(Project obj){
		if(oprate)
			return new ProjectDao().update(obj);
		return false;
	}

	public boolean del(Long id){
		if(oprate)
			return new ProjectDao().del(id);
		return false;
	}

	/**
	 * 获取项目名 数组
	 * @return
     */
	public  String[] getProjectNames(){
		List<Project> objs = new ProjectDao().query(null);
		if(objs.size() == 0){
			return new String[]{""};
		}
		String[] results = new String[objs.size()];
		for(int i=0; i < objs.size(); i++){
			results[i] = objs.get(i).getName();
		}
		return  results;
	}
}
